module core.events;

import tango.util.container.LinkedList;

/*
 * *************** Events
 */

enum EventTypes {
	mouse,
	key,
	system // ?
}

struct Event {
	EventTypes type;

	union {
		KeyboardEvent key;
		MouseEvent mouse;
		SystemEvent system;
	}
}

/*
 * *************** System event
 */

enum SystemEvents {
	shutdown
}

struct SystemEvent {
	SystemEvents type;
}

/*
 * *************** Mouse event 
 */

enum MouseEvents {
	pressed,
	released,
	clicked
}

struct MouseEvent {
	MouseEvents type;

	int btn;
	uint x = 0, y = 0;
}

/*
 * *************** Keyboard event
 */

enum KeyboardEvents {
	pressed,
	released
}

enum KeyCodes {
	Up,
	Down,
	Left,
	Right,
	Space,
	Backspace,
	Shift,
	Esc,
	Return,
	Tab,
	Alt,
	Ctrl,
	Insert,
	Delete,
	Home,
	End,
	PageUp,
	PageDown,
	F1,
	F2,
	F3,
	F4,
	F5,
	F6,
	F7,
	F8,
	F9,
	F10,
	F11,
	F12,
	Q,
	W,
	E,
	R,
	T,
	Y,
	U,
	I,
	O,
	P,
	A,
	S,
	D,
	F,
	G,
	H,
	J,
	K,
	L,
	Z,
	X,
	C,
	V,
	B,
	N,
	M
}

struct KeyboardEvent {
	KeyboardEvents type;
	KeyCodes code;
}

/*
 * *************** Interfaces part
 */

interface IEventProcessor {
	void processEvent(Event);
}

class EventFeeder {
	private:
		LinkedList!(IEventProcessor) processors;
		LinkedList!(Event) events;

	public:
		this() {
			processors = new LinkedList!(IEventProcessor);
			events = new LinkedList!(Event);
		}

		/**
		 * Subscribe event processor to the current event feed.
		 * Params:
		 *     proc =
		 */
		void addProcessor(IEventProcessor proc) {
			processors.add(proc);
		}

		void enqueueEvent(Event e) {
			events.add(e);
		}

		void flush() {
			if(events.size() == 0)
				return;

			foreach(p; processors) {
				foreach(e; events) {
					p.processEvent(e);
				}
			}
		}
}
